package com.hywood.manager.live.configure;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class Swagger2Config {
    @Value("${spring.profiles.active}")
    private String env;

    @Bean
    public Docket createRestApi() {
        if(("prd").equals(env)) {
            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfoOnline())
                    .select()
                    .paths(PathSelectors.none())//如果是线上环境，添加路径过滤，设置为全部都不符合
                    .build();
        }else {
            ParameterBuilder headerPar = new ParameterBuilder();
            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfo())
                    .select()
                    .apis(RequestHandlerSelectors.basePackage("com.hywood.manager.live"))// 扫描该包下的所有需要在Swagger中展示的API，@ApiIgnore注解标注的除外
                    .paths(PathSelectors.any())
                    .build();
        }
    }

    private ApiInfo apiInfo() {// 创建API的基本信息，这些信息会在Swagger UI中进行显示
        return new ApiInfoBuilder()
                .title("live-manager-service提供的RESTful APIs")// API 标题
                .description("live-manager-service提供的RESTful APIs")// API描述
                .contact(new Contact("HuJian","","luoboflower@hotmail.com"))
                .version("1.0")// 版本号
                .build();
    }

    private ApiInfo apiInfoOnline() {   //线上环境禁止swagger显示
        return new ApiInfoBuilder()
                .title("")
                .description("")
                .license("")
                .licenseUrl("")
                .termsOfServiceUrl("")
                .version("")
                .contact(new Contact("","", ""))
                .build();
    }

}
